package com.egdigital.allzic.ui.fragment;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.egdigital.allzic.R;
import com.egdigital.allzic.data.json.JsonCallBack;
import com.egdigital.allzic.data.json.JsonParserStation;
import com.egdigital.allzic.data.json.VolleyManager;
import com.egdigital.allzic.data.object.Station;
import com.egdigital.allzic.ui.activity.ActivityMain;
import com.egdigital.allzic.ui.adapter.AdapterGridAllzic;
import com.egdigital.allzic.utils.NetworkManager;
import com.egdigital.allzic.utils.Utils;

public class FragmentTabAllzik extends BaseMainFragment implements OnItemClickListener, JsonCallBack, OnClickListener {

	private static final String	TAG	= FragmentTabAllzik.class.getSimpleName();	// log
	// class data
	private JsonParserStation	mJsonParser;
	private GridView			mGridView;
	private AdapterGridAllzic	mAdapter;
	// network ui
	protected ActivityMain		mParent;
	protected View				mView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_tab_allzic, container, false);
		mLoadingView = (RelativeLayout) mView.findViewById(R.id.fragment_allzic_loading);
		mReloadView = (LinearLayout) mView.findViewById(R.id.fragment_allzic_reload);
		mNoResultView = (TextView) mView.findViewById(R.id.fragment_allzic_noresult);
		mGridView = (GridView) mView.findViewById(R.id.fragment_allzik_gridview);
		return mView;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		mGridView.setOnItemClickListener(this);
		mReloadView.setOnClickListener(this);
	}

	@Override
	public void onResume() {
		super.onResume();
		if (mJsonParser == null) {
			mJsonParser = new JsonParserStation();
			VolleyManager.init(mParent);
			mJsonParser.initAndStartRequestQueue();
			mJsonParser.setJsonCallBack((JsonCallBack) this);
		}
		if (mParent.getmListAllZic() == null || mParent.getmListAllZic().isEmpty()) {
			loadData();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
		if (!NetworkManager.isConnected(mParent.getApplicationContext())) { // test network
			Utils.customToast(new SpannableStringBuilder(getString(R.string.no_network)), mParent.getApplicationContext(), false);
		} else {
			Station station = mParent.getmListAllZic().get(mAdapter.getRealPosition(position));
			mParent.prepareToPlay(station);
			mParent.showPlayerBanner();
			mParent.updatePlayerBanner();
		}
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mParent = (ActivityMain) activity;
	}

	@Override
	public void onDetach() {
		super.onDetach();
		mParent = null;
	}

	private void loadData() {
		if (NetworkManager.isConnected(mParent.getApplicationContext())) {
			mJsonParser.extractJsonObjectData(getString(R.string.url_allzic));
		} else {
			setReload(mGridView);
		}
	}

	private void updateAdapter(List<Station> list) {
		mAdapter = null;
		mAdapter = new AdapterGridAllzic(list, mParent.getApplicationContext());
		mGridView.setVisibility(View.VISIBLE);
		mGridView.setAdapter(mAdapter);
	}

	@Override
	public void downloadStarted() {
		setLoading(mGridView);
	}

	@Override
	public void downloadPercent(int percent) {
		// do nothing
	}

	@Override
	public <T> void downloadCompleted(List<T> list) {
		if (mParent == null || list == null) {
			return;
		}
		if (!list.isEmpty()) {
			setLoaded(mGridView);
			mParent.setmListAllZic((List<Station>) list);
			updateAdapter((List<Station>) list);
		} else {
			setReload(mGridView);
		}
	}

	@Override
	public void downloadEndedWithError(VolleyError error) {
		setReload(mGridView);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.fragment_allzic_reload:
				loadData();
				break;
		}
	}
}
